package com.pmcc.cj.socket;


import com.pmcc.cj.entity.CjCustomer;
import com.pmcc.cj.entity.CjOrder;
import com.pmcc.cj.entity.CjTask;
import com.pmcc.cj.repository.*;
import com.pmcc.cj.service.threads.MyThreadRobot;
import com.pmcc.core.repository.SmsRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 姚楠 on 2018/3/16.
 */
@Component
public class DeviceStateMap implements ApplicationRunner {

    @Autowired
    CjOrderRepository cjOrderRepository;
    @Autowired
    CjCustomerRepository cjCustomerRepository;
    @Autowired
    SmsRecordRepository smsRecordRepository;
    @Autowired
    CjTaskRepository cjTaskRepository;
    @Autowired
    CjCommodityYunRepository yunRepository;
    @Autowired
    CjOrderDetailRepository cjOrderDetailRepository;

    /**
     * 项目启动后自动执行的方法
     * 给机手未接单的添加新的线程
     *
     * @param var1 全局参数
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments var1)   {
        try {
            List<CjTask> list= cjTaskRepository.findByFettleType(0);
//        System.out.println("***************************************************************************************************************************************************"+list.size());
            if(list != null && list.size()>0){
                for(CjTask task: list){
                    try {
                        CjOrder order =cjOrderRepository.findOne(task.getOrderId());
                        String[] str= order.getRobotThread().split(",");
                        List<CjCustomer> lt1= new ArrayList<>();
                        if(str != null && str.length>0){
                            for(String id: str){
                                CjCustomer customer =cjCustomerRepository.findOne(id);
                                lt1.add(customer);
                            }
                        }
                        List<CjOrder> lt2 =new ArrayList<>();
                        lt2.add(order);
                        Thread thread = new Thread(new MyThreadRobot(smsRecordRepository,lt1,lt2,cjTaskRepository,yunRepository,cjOrderDetailRepository,cjOrderRepository));
                        thread.start();
                    }catch (Exception e){
                        //一个订单出问题，不影响其他订单的推送
                        continue;
                    }
                }
            }
        }catch (Exception e){

        }

    }




}
